package com.lg.baselibrary.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by LiGang on 2017/5/27.
 */

public class BaseViewHolder extends RecyclerView.ViewHolder{
    protected SparseArray<View> mViewsCache;
    private SparseArray<View.OnClickListener> onClickListenerCaches;
    protected View mItemView;
    private Context mContext;

    public BaseViewHolder(Context context,View itemView) {
        super(itemView);
        this.mContext = context;
        mViewsCache = new SparseArray<>();
        this.mItemView = itemView;
    }
    public int getItemPosition(){
        return getAdapterPosition();
    }


    public  <T extends View> T findView(int viewId){
        View view = getViewFromCache(viewId);
        if(view == null){
            view = mItemView.findViewById(viewId);
            cacheView(viewId,view);
        }
        return (T) view;
    }

    private View getViewFromCache(int viewId){
        return mViewsCache.get(viewId);
    }
    private void cacheView(int viewId,View view){
        mViewsCache.put(viewId,view);
    }

    /**
     * 为item中的TextView或其子类setText
     * @param viewId TextView或其子类id
     * @param text
     * @return
     */
    public BaseViewHolder setText(int viewId,CharSequence text){
        TextView textView = findView(viewId);
        textView.setText(text);
        return this;
    }

    /**
     * 为item中的TextView或其子类setText
     * @param viewId TextView或其子类id
     * @param textResId String资源id
     * @return
     */
    public BaseViewHolder setText(int viewId,int textResId){
        String text = mContext.getString(viewId);
        return setText(viewId,text);
    }
    public BaseViewHolder setTextColor(int viewId,int textColor){
        TextView textView = findView(viewId);
        textView.setTextColor(textColor);
        return this;
    }
    public BaseViewHolder setBackgroundColor(int viewId,int backgroundColor){
        View view = findView(viewId);
        view.setBackgroundColor(backgroundColor);
        return this;
    }
    public BaseViewHolder setBackgrounResource(int viewId, int resId) {
        View view = findView(viewId);
        view.setBackgroundResource(resId);
        return this;
    }
    /**
     * 设置显隐，并返回this
     */
    public BaseViewHolder setViewVisibility(int viewId, int visibility) {
        View view = findView(viewId);
        view.setVisibility(visibility);
        return this;
    }
    /**
     * 设置是否可用，并返回this
     */
    public BaseViewHolder setEnabled(int viewId, boolean enabled) {
        View view = findView(viewId);
        view.setEnabled(enabled);
        return this;
    }
    /**
     * 设置ImageView的图片，并返回this
     */
    public BaseViewHolder setImageResource(int viewId, int resId) {
        ImageView iv = findView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    /**
     * 设置ImageView的图片，并返回this
     */
    public BaseViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView iv = findView(viewId);
        iv.setImageBitmap(bitmap);
        return this;
    }
    public BaseViewHolder setImageLevel(int viewId,int level){
        ImageView iv = findView(viewId);
        iv.setImageLevel(level);
        return this;
    }

    public void setOnClickListener(int viewId, final OnClickListener onClickListener) {
        if(onClickListenerCaches == null){
            onClickListenerCaches = new SparseArray<>();
        }
        View view = findView(viewId);
        View.OnClickListener clickListener = onClickListenerCaches.get(viewId);
        if(clickListener == null){
            clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onClickListener != null){
                        onClickListener.onClick(v,getItemPosition());
                    }
                }
            };
            onClickListenerCaches.put(viewId,clickListener);
            view.setOnClickListener(clickListener);
        }
    }

    public interface OnClickListener{
        void onClick(View view,int position);
    }

}
